package com.acertificate.excellentarticle;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class Excellentarticle {
	
	public static final String URL_PREFIX = "http://www.duwenzhang.com/wenzhang/yingyuwenzhang/list_10_";
	public static final String SUFFIX = ".html";

	public static final String SQL_TEMPLATE = "insert into cms_cms values(null,'%s','','%s',12,%d,'',1,0,1,'',1,0,'','',0,0,1,1);";

	public static int maxProcess = 20;
	public static int total = 6;
	public static int threadCount;

	{
		threadCount = total / maxProcess + (total % maxProcess == 0 ? 0 : 1);
		System.out.println(threadCount);
	}

	public static void main(String[] args) throws ParserException, MalformedURLException, Exception {
		Excellentarticle excellentarticle = new Excellentarticle();
		for (int i = 1; i <= threadCount; i++) {
			ExcellentarticleThread thread = excellentarticle.new ExcellentarticleThread((i - 1) * maxProcess + 1, (i
					* maxProcess > total ? total : i * maxProcess));
			thread.start();
		}
	}

	class ExcellentarticleThread extends Thread {

		public int start;
		public int end;

		public ExcellentarticleThread() {
		}

		public ExcellentarticleThread(int start, int end) {
			this.start = start;
			this.end = end;
		}

		public void run() {
			for (int i = start; i < end; i++) {
				getManifest(i, URL_PREFIX + i + SUFFIX);
			}
		}

		public void getManifest(int x, String url) {
			System.out.println(url);
			List<Map<String, String>> results = new ArrayList<Map<String, String>>();
			try {
				URLConnection urlConnection = new URL(url).openConnection();
				String content1 = IOUtils.toString(urlConnection.getInputStream());
				Parser contentParser = new Parser();
				contentParser.setInputHTML(content1);

				NodeList xx = contentParser.parse(new HasAttributeFilter("class", "ulink"));
				for (int i = 0; i < xx.size(); i++) {
					LinkTag node = (LinkTag) xx.elementAt(i);
					if (node.getLink().contains(".html")) {
						String txt = node.toPlainTextString();
						txt = txt.replace("'", "\\'");
						txt = txt.replace("\"", "\\\"");
						System.out.println(txt);
						Map<String, String> map = new HashMap<String, String>();
						map.put("title", txt);
						map.put("content", getContent(node.getLink()));
						results.add(map);
					}

				}

				StringBuffer sb = new StringBuffer();
				for (Map<String, String> map : results) {
					sb.append(String.format(SQL_TEMPLATE,
							new Object[] { map.get("title"), map.get("content"), System.currentTimeMillis() }));
					sb.append("\r\n");
				}
				try {
					String fileName = x + "-" + System.currentTimeMillis() + ".txt";
					System.out.println("generating SQL files " + fileName + " ...");
					IOUtils.write(sb.toString(), new FileOutputStream(new File("c:/excellentarticle/" + fileName)));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		public String getContent(String url) throws Exception {
			String content = null;
			Parser contentParser = new Parser();
			URLConnection urlConnection = new URL(url).openConnection();
			String content1 = IOUtils.toString(urlConnection.getInputStream());
			contentParser.setInputHTML(content1.replace("", ""));
			NodeList nodes = contentParser.parse(new HasAttributeFilter("id", "wenzhangziti"));
			for (int i = 0; i < nodes.size(); i++) {
				Node node = nodes.elementAt(i);
				content = node.toHtml();
				content = content.replace("'", "\\'");
				content = content.replace("\"", "\\\"");
			}
			return content;
		}
	}

}
